{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 学习笔记"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 一.基础"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 不换行输出，在变量后加入end=\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "test1test2"
     ]
    }
   ],
   "source": [
    "a=\"test1\"\n",
    "b='test2'\n",
    "print(a,end=\"\")\n",
    "print(b,end=\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 输出格式化字符串\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x=4\n"
     ]
    }
   ],
   "source": [
    "x=4\n",
    "print('x=%d' % x)#注意是%"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 二.程序的控制结构"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### if 语句\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: ''",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "\u001B[1;32m~\\AppData\\Local\\Temp/ipykernel_23980/1461888519.py\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[1;32m----> 1\u001B[1;33m \u001B[0ma\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0minput\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m'please enter a number1:'\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m      2\u001B[0m \u001B[0mb\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0minput\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m'please enter a number2:'\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      3\u001B[0m \u001B[1;32mif\u001B[0m \u001B[0ma\u001B[0m\u001B[1;33m>=\u001B[0m\u001B[0mb\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      4\u001B[0m     \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m'差值'\u001B[0m\u001B[1;33m,\u001B[0m\u001B[0ma\u001B[0m\u001B[1;33m-\u001B[0m\u001B[0mb\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      5\u001B[0m \u001B[1;32melif\u001B[0m \u001B[0mb\u001B[0m\u001B[1;33m>\u001B[0m\u001B[0ma\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;31mValueError\u001B[0m: invalid literal for int() with base 10: ''"
     ]
    }
   ],
   "source": [
    "a=int(input('please enter a number1:'))\n",
    "b=int(input('please enter a number2:'))\n",
    "if a>=b:\n",
    "    print('差值',a-b)\n",
    "elif b>a:\n",
    "    print('差值：',b-a)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 布尔表达式  bool()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "True+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "bool([])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "#### 函数range()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "r=range(10)#创建的为整数序列,可以为三个参数range(开始，结束，步长)\n",
    "print(r)\n",
    "print(list(r))#查看整数序列\n",
    "print(10 in r)#查看某个数是否在这个序列中，in  ,not in"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### while"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "初始化变量\n",
    "条件判断\n",
    "条件执行体\n",
    "改变变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "a=0\n",
    "sum=0\n",
    "\n",
    "while a<10:\n",
    "    sum+=a\n",
    "    a+=1\n",
    "print('和为',sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1-100的偶数和： 2500\n"
     ]
    }
   ],
   "source": [
    "a = 0\n",
    "sum = 0\n",
    "\n",
    "while a < 101:\n",
    "    if a % 2:  # if a%2:奇数         if not bool(a % 2)：偶数 if a%2==0：\n",
    "        sum += a\n",
    "    a += 1\n",
    "\n",
    "print('1-100的偶数和：', sum)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### for in"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "for i in range(10):     #遍历序列\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "for _ in range(5):     #可用range作为打印次数\n",
    "    print('人生苦短，且行且歌')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "a=0\n",
    "for i in range(100):\n",
    "    if i%2==0:\n",
    "        a+=i\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "for i in range(100,1000):    #水仙花数\n",
    "    ge = i%10\n",
    "    shi =i//10%10\n",
    "    bai =i//100\n",
    "    if ge**3+shi**3+bai**3==i:\n",
    "        print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "for i in range(3):\n",
    "    pwd=input('请输入密码：')\n",
    "    if pwd=='8888':\n",
    "        print('密码正确')\n",
    "        break\n",
    "    else:\n",
    "        print('密码错误')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "a = \"床前明月光，疑是地上霜。举头望明月，低头思故乡\"\n",
    "\n",
    "for i in a:\n",
    "    if i ==\"月\":\n",
    "        print(\"发现汉字：\",i)\n",
    "        break\n",
    "    else:\n",
    "         print(\"无\")\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 嵌套循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "for i in range(1,10):      #行\n",
    "    for j in range(1,10):  #列  for j in range(1,i+1):\n",
    "        if i>=j:     #输出三角形，去掉则为矩形\n",
    "            print(i,\"*\",j,\"=\",i*j,end='\\t')  #换行输出，并添加空格\n",
    "    print()  #不换行"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "source": [
    "## 三.序列\n",
    "### 列表   [ ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "list1=['1',2,'3',4]\n",
    "print(list1.index(2))    #根据列表中元素来查找索引位置\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "list1=['1',2,'3',4]\n",
    "print(list1[2])       #根据索引确定是何元素\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "lst=[10,20,30,40,50,60,70,80,90,100]\n",
    "\n",
    "print(lst[1:6:1])\n",
    "print(lst[:])\n",
    "print(lst[::-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 列表添加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "lst=[1,2,3,4,5,6,7,8,9,10,'J','Q','K','A']\n",
    "\n",
    "lst.append(23)    #列表末尾添加元素\n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "lst=[1,2,3,4,5,6,7,8,9,10,'J','Q','K','A']\n",
    "lst2=[98,97,96]\n",
    "lst.append(lst2) #添加另一个列表到旧列表\n",
    "print(lst)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "lst=[1,2,3,4,5,6,7,8,9,10,'J','Q','K','A']\n",
    "lst.extend(lst2)\n",
    "print(lst)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "lst=[1,2,3,4,5,6,7,8,9,10,'J','Q','K','A']\n",
    "lst.insert(2,66) #在任意位置添加一个元素\n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "lst=[1,2,3,4,5,6,7,8,9,10,'J','Q','K','A']\n",
    "lst3=[True,False,'hello']\n",
    "lst[6:9]=lst3  #利用切片来在任意位置添加多个元素\n",
    "print(lst)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 删除列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "lst=[1,2,3,4,5,6,7,8,9,10,'J','Q','K','A']\n",
    "lst.remove(5)   #移除元素，重复只删除第一个\n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "lst=[1,2,3,4,5,6,7,8,9,10,'J','Q','K','A']\n",
    "lst.pop(2)  #通过索引来删除，不指定参数将删除最后一个元素pop()\n",
    "print(lst)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "lst=[1,2,3,4,5,6,7,8,9,10,'J','Q','K','A']\n",
    "new_lst=lst[1:5]   #产生一个新的列表对象\n",
    "print(lst)\n",
    "print(new_lst)\n",
    "\n",
    "lst[1:5]=[]  #删除原列表的内容，不产生新的列表\n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "lst=[1,2,3,4,5,6,7,8,9,10,'J','Q','K','A']\n",
    "lst[0]=\"king\"   #列表元素的替换\n",
    "print(lst)\n",
    "\n",
    "lst[0:4]=['king','xx','yy','zz']  #同时替换多个列表内容\n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 列表排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "lst=[1,66,3,4,5,6,65,8,9,10]\n",
    "lst.sort()   #不产生新列表\n",
    "print(lst)\n",
    "\n",
    "lst.sort(reverse=True)  #reverse=True为降序排列\n",
    "print(lst)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "lst=[1,2,3,4,5,99,7,8,9,10]\n",
    "lst2=sorted(lst)  #调用内置函数，产生一个新列表，可指定参数sorted(lst,reverse=True)\n",
    "print(lst)\n",
    "print(lst2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 列表删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "lst = [i for i in range(1, 10)]\n",
    "print(lst)\n",
    "\n",
    "lst.copy()  #复制一遍列表\n",
    "print(lst)\n",
    "\n",
    "lst.append(5)\n",
    "print(lst)\n",
    "\n",
    "b=len(lst)     #\n",
    "print(b)\n",
    "print(max(lst))\n",
    "print(min(lst))\n",
    "\n",
    "a=lst.count(5)  #元素出现个数\n",
    "print(a)\n",
    "\n",
    "lst.reverse()   #两极反转\n",
    "print(lst)\n",
    "\n",
    "lst.clear()  #删除列表中内容\n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 列表生成式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "lst = [i for i in range(1, 10)]\n",
    "print(lst)\n",
    "lst2 = [i ** 3 for i in range(1, 10)]\n",
    "print(lst2)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字典  { }"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "score={'王':100,'一':99,'帆':98}\n",
    "print(score)\n",
    "\n",
    "print(score['王'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 获取元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "score={'王':100,'一':99,'帆':98}\n",
    "print(score)\n",
    "\n",
    "print(score['王'])  #若键值不存在会报错\n",
    "print(score.get('王'))\n",
    "print(score.get(\"垃圾\",'查无此人'))#若指定键值不存在，可设置，默认输出\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 字典删除新增"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "score={'王':100,'一':99,'帆':98}\n",
    "print(score)\n",
    "\n",
    "print('垃圾' in score) #判断键值是否在字典中\n",
    "\n",
    "del score['王']  #删除一对\n",
    "print(score)\n",
    "\n",
    "score.clear()  #清空字典内容\n",
    "print(score)\n",
    "\n",
    "score['wang']=100\n",
    "print(score)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 获取字典视图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "score={'王':100,'一':99,'帆':98}\n",
    "print(score)\n",
    "\n",
    "keys = score.keys()\n",
    "print(keys)\n",
    "print(list(keys))  #将视图转为列表\n",
    "\n",
    "values = score.values()\n",
    "print(values)\n",
    "\n",
    "items = score.items()\n",
    "print(items)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 遍历"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "score={'王':100,'一':99,'帆':98}\n",
    "\n",
    "for i in score:  # 获取的是字典中的键\n",
    "    print(i)\n",
    "    print(i,score[i],score.get(i)) #输出字典的值\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 特点\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "无序；\n",
    "key不可以重复；\n",
    "key为不可变对象，不能用列表做键值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 字典生成式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "items=['fruits','books','others']\n",
    "keys=[55,66,99]\n",
    "\n",
    "d={item.upper():price   for item ,price in zip(items,keys)}\n",
    "print(d)\n",
    "print(type(d))\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### 元组 ( )\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('python', 'world', 98)\n"
     ]
    }
   ],
   "source": [
    "t=('python','world',98)#多个元素加小括号\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('p', 'y', 't', 'h', 'o', 'n')\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "t=tuple(('python'))  #使用内置函数\n",
    "print(t)\n",
    "print(type(t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('python',)\n",
      "<class 'tuple'>\n",
      "python\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "t=('python',) #单个元素要加逗号，不然为字符串\n",
    "print(t)\n",
    "print(type(t))\n",
    "\n",
    "t=('python') #单个元素要加逗号，不然为字符串\n",
    "print(t)\n",
    "print(type(t))\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 元组的遍历"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "y\n",
      "w\n",
      "y\n",
      "f\n"
     ]
    }
   ],
   "source": [
    "t=('w','y','f')\n",
    "print(t[1])\n",
    "\n",
    "for i in t:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 集合 { }"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{2, 3, 4, 5, 6, 7, 8, 9}\n"
     ]
    }
   ],
   "source": [
    "s={2,3,4,5,5,6,6,7,7,7,9,8,}\n",
    "print(s)  #集合中的元素不允许重复"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 2, 3, 4, 5}\n"
     ]
    }
   ],
   "source": [
    "s=set(range(6))  #内置函数使用，也可创建空集合\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "#### 基操\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "s={1,8,7,9,3,6,4,5,2}\n",
    "print(1 in s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5, 6, 7, 8, 9, 99}\n"
     ]
    }
   ],
   "source": [
    "s={1,8,7,9,3,6,4,5,2}\n",
    "s.add(99)   #一次添加一个\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13}\n"
     ]
    }
   ],
   "source": [
    "s={1,8,7,9,3,6,4,5,2}\n",
    "s.update((11,12,13))   #添加多个\n",
    "print(s)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 3, 4, 5, 6, 7, 8, 9}\n"
     ]
    }
   ],
   "source": [
    "s={1,8,7,9,3,6,4,5,2}\n",
    "s.remove(2)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5, 6, 7, 8, 9}\n"
     ]
    }
   ],
   "source": [
    "s={1,8,7,9,3,6,4,5,2}\n",
    "s.discard(100)   #删除不存在元素不会报错\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{2, 3, 4, 5, 6, 7, 8, 9}\n",
      "set()\n"
     ]
    }
   ],
   "source": [
    "s={1,8,7,9,3,6,4,5,2}\n",
    "s.pop()\n",
    "print(s)\n",
    "s.clear()\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 集合关系"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "s={1,8,7,9,3,6,4,5,2}\n",
    "s1={1,2}\n",
    "print(s==s1)    # ==,!="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "''' 一个集合是否是另一个集合的子集，超集，是否交集'''\n",
    "s={1,8,7,9,3,6,4,5,2}\n",
    "s1={1,2}\n",
    "\n",
    "print(s.issubset(s1)) #s是s1的子集吗\n",
    "print(s1.issubset(s))\n",
    "\n",
    "print(s.issuperset(s1))# s是s1的超集吗\n",
    "\n",
    "print(s.isdisjoint(s1))#s与s1没有交集吗"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 数学运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2}\n",
      "{1, 2}\n"
     ]
    }
   ],
   "source": [
    "s={1,8,7,9,3,6,4,5,2}\n",
    "s1={1,2,10,12}\n",
    "print(s.intersection(s1)) #求交集\n",
    "print(s&s1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12}\n",
      "{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12}\n"
     ]
    }
   ],
   "source": [
    "s={1,8,7,9,3,6,4,5,2}\n",
    "s1={1,2,10,12}\n",
    "\n",
    "print(s.union(s1))  #求并集\n",
    "print(s|s1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{3, 4, 5, 6}\n",
      "{3, 4, 5, 6}\n"
     ]
    }
   ],
   "source": [
    "s={1,3,6,4,5,2}\n",
    "s1={1,2,10,12}\n",
    "\n",
    "print(s.difference(s1))\n",
    "print(s-s1)#求差集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{3, 4, 5, 6, 10, 12}\n"
     ]
    }
   ],
   "source": [
    "s={1,3,6,4,5,2}\n",
    "s1={1,2,10,12}\n",
    "\n",
    "print(s.symmetric_difference(s1))#对称差集\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 集合生成式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 64, 8, 27}\n"
     ]
    }
   ],
   "source": [
    "s={i**3 for i in range(5)}\n",
    "print(s)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 总结"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "列表   可变    可重复    有序    []\n",
    "元组   不可变   可重复    有序   （）\n",
    "集合   可变    不可重复   无序   {}\n",
    "字典   可变   key不可重复   无序  {key：value}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 四.字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 常用操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1\n",
      "-1\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "'''查询'''\n",
    "s='hello,hello'\n",
    "'''查询最先出现的'''\n",
    "print(s.find(\"k\"))#未找到返回 -1\n",
    "#print(s.index(\"d\"))#未找到ValueError: substring not found\n",
    "'''查询最后出现的'''\n",
    "print(s.rfind(\"f\"))#未找到返回-1\n",
    "print(s.rindex(\"h\"))\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello,helLo\n",
      "HELLO,HELLO\n",
      "hello,hello\n",
      "Hello,Hello\n",
      "HELLO,HELlO\n",
      "Hello,hello\n"
     ]
    }
   ],
   "source": [
    "'''字符串大小写转换'''\n",
    "\n",
    "s='hello,helLo'\n",
    "print(s)\n",
    "print(s.upper())  #转换之后会产生新的字符串\n",
    "print(s.lower())\n",
    "print(s.title())  #每个单词开头大写\n",
    "print(s.swapcase())  #两极反转\n",
    "print(s.capitalize()) # 只第一个大写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****hello,python****\n",
      "hello,python********\n",
      "        hello,python\n",
      "00000000hello,python\n"
     ]
    }
   ],
   "source": [
    "'''字符串内容对齐'''\n",
    "s='hello,python'\n",
    "print(s.center(20,\"*\"))  #中间对齐\n",
    "print(s.ljust(20,'*'))#左对齐\n",
    "print(s.rjust(20))#右对齐\n",
    "print(s.zfill(20))#右对齐，通过0补全"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['hello', 'python']\n",
      "['hello', 'python', 'world']\n",
      "['hello', 'python.world']\n"
     ]
    }
   ],
   "source": [
    "'''字符串的分割'''\n",
    "'''s.rsplit()为从右边分割'''\n",
    "s='hello python'\n",
    "lst = s.split() #默认分隔符为空格\n",
    "print(lst)\n",
    "\n",
    "s=\"hello.python.world\"\n",
    "lst=s.split(sep=\".\")\n",
    "print(lst)\n",
    "\n",
    "lst1=s.split(sep=\".\",maxsplit=1)#指定参数\n",
    "print(lst1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "'''判断字符串'''\n",
    "s=\"python\"\n",
    "print(s.isidentifier())\n",
    "#指定字符串是不是合法的标识符\n",
    "print(\"fshh\".isalpha())\n",
    "#指定字符串是不是全部由字母组成\n",
    "#isspace\n",
    "#isdecimal\n",
    "#ismumeric\n",
    "#isalum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello,python\n",
      "G,G,G,h,oo,l\n",
      "h.e.l.l.o\n",
      "hello\n",
      "helloworld\n",
      "hello*world\n",
      "n*i*c*e\n"
     ]
    }
   ],
   "source": [
    "'''字符串的替换和合并'''\n",
    "\n",
    "s='hello,python'\n",
    "print(s.replace('hello',\"Hello\"))#后者替换前者\n",
    "\n",
    "s1='h,h,h,h,oo,l'\n",
    "print(s1.replace('h','G',3))#可指定最大替换次数\n",
    "\n",
    "'''将列表或元组中的字符串合并'''\n",
    "\n",
    "ls=['h','e','l','l','o']\n",
    "print(\".\".join(ls))\n",
    "print(\"\".join(ls))#可指定连接符\n",
    "\n",
    "t=('hello','world')\n",
    "print(''.join(t))\n",
    "print('*'.join(t))\n",
    "\n",
    "print('*'.join(\"nice\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "108 111\n",
      "29579\n"
     ]
    }
   ],
   "source": [
    "'''字符串的比较'''\n",
    "\n",
    "print(\"apple\">\"appoa\")\n",
    "print(ord('l'),ord('o'))\n",
    "print(ord(\"王\"))\n",
    "'''==与is区别\n",
    "==比较的是value\n",
    "is 比较的是id\n",
    "'''"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 字符串切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello,my\n",
      "20\n",
      "hlom aei\n",
      "fyw si eman ym,olleh\n"
     ]
    }
   ],
   "source": [
    "'''（不具备增删改操作，产生一个新的字符串'''\n",
    "\n",
    "s='hello,my name is wyf'\n",
    "s1=s[:8]\n",
    "s2=s[0:15:2]#可设置开始结束步长\n",
    "print(s1)\n",
    "print(len(s))\n",
    "print(s2)\n",
    "print(s[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "ename": "IndentationError",
     "evalue": "unexpected indent (Temp/ipykernel_25144/572903430.py, line 9)",
     "output_type": "error",
     "traceback": [
      "\u001B[1;36m  File \u001B[1;32m\"C:\\Users\\wyf\\AppData\\Local\\Temp/ipykernel_25144/572903430.py\"\u001B[1;36m, line \u001B[1;32m9\u001B[0m\n\u001B[1;33m    print('我是{0}，今年{1}岁'.format(name,age))\u001B[0m\n\u001B[1;37m    ^\u001B[0m\n\u001B[1;31mIndentationError\u001B[0m\u001B[1;31m:\u001B[0m unexpected indent\n"
     ]
    }
   ],
   "source": [
    "'''格式化字符串'''\n",
    "\n",
    "'''%'''\n",
    "name = 'wyf'\n",
    "age = 19\n",
    "print('my name is %s,now is %d' % (name,age))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我是wyf,今年10岁\n"
     ]
    }
   ],
   "source": [
    "'''{}'''\n",
    "name =\"wyf\"\n",
    "age=10\n",
    "print('我是{0},今年{1}岁'.format(name,age))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "my name is wyf,now is 10\n"
     ]
    }
   ],
   "source": [
    "'''f-strings'''\n",
    "name =\"wyf\"\n",
    "age=10\n",
    "print(f'my name is {name},now is {age}')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      3.21\n",
      "3.1415926\n",
      "3.14\n",
      "     3.142\n"
     ]
    }
   ],
   "source": [
    "print('%10.2f' % 3.2133)#宽度为10，小数点后两位\n",
    "\n",
    "print('{}'.format(3.1415926))\n",
    "print('{0:.3}'.format(3.1415926))#表示总共三位\n",
    "print('{0:10.3f}'.format(3.1415926))#宽度为10，小数点后三位"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 字符串的编码转换\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'\\xcc\\xec\\xd1\\xc4\\xb9\\xb2\\xb4\\xcb\\xca\\xb1'\n",
      "b'\\xe5\\xa4\\xa9\\xe6\\xb6\\xaf\\xe5\\x85\\xb1\\xe6\\xad\\xa4\\xe6\\x97\\xb6'\n",
      "天涯共此时\n"
     ]
    }
   ],
   "source": [
    "s='天涯共此时'\n",
    "\n",
    "print(s.encode(encoding='GBK'))\n",
    "print(s.encode(encoding='UTF-8'))\n",
    "\n",
    "byte=s.encode(encoding='UTF-8')\n",
    "print(byte.decode(encoding=\"UTF-8\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 五.函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 函数的创建与调用"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PyCharm (test1)",
   "language": "python",
   "name": "pycharm-6c3bae1e"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}